package de.cbf.cam.dsa.tavern.carte.model.entities;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import java.beans.PropertyChangeSupport;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Iterator;

import de.cbf.cam.dsa.tavern.carte.model.entities.interfaces.INamedEntity;

public class Meal implements INamedEntity{
	
	public static final int PCE_INDEX_name					=  1;	
	public static final int PCE_INDEX_type					=  2;
	public static final int PCE_INDEX_description			=  3;
	public static final int PCE_INDEX_ingredients			=  4;
	public static final int PCE_INDEX_price					=  5;
	
	public final PropertyChangeSupport propertyChangeSupport = new PropertyChangeSupport(this);
	private PropertyChangeAdvancement pca = new PropertyChangeAdvancement();
	
	private String 					name		= "";
	private MealType 				type		= null;
	private String 					description = "";
	private Double 					price 		= 0.0;
	private Collection<Ingredient> 	ingredients = new ArrayList<Ingredient>();
		
	public Meal() {
		super();	
	}

	public static Collection<Object> transformMealIterator(
			Iterator<Meal> iterator) {
		Collection<Object> resultList = new ArrayList<Object>();		
		while (iterator.hasNext()) {
			Meal meal = (Meal) iterator.next();
			resultList.add(meal);
		}
		return resultList;	
	}
	
	@Override
	public String toString() {	
		return name;
	}
	
	public String getName() {
		return name;
	}

	public void setName(String name) {
		if(this.name != null && this.name.equals(name)) return;
		String nameOld = this.name;
		this.name = name;		
		propertyChangeSupport.fireIndexedPropertyChange("name", PCE_INDEX_name,nameOld,name);
	}

	public String getDescription() {
		return description;
	}

	public void setDescription(String description) {
		if(this.description != null && this.description.equals(description)) return;
		String descriptionOld = this.description;
		this.description = description;	
		propertyChangeSupport.fireIndexedPropertyChange
			("description", PCE_INDEX_description,descriptionOld,description);		
	}

	public Double getPrice() {
		return price;
	}

	public void setPrice(Double price) {
		if(this.price != null && this.price.equals(price)) return;
		Double priceOld = this.price;
		this.price = price;	
		propertyChangeSupport.fireIndexedPropertyChange("price", PCE_INDEX_price,priceOld,price);		
	}
	public MealType getType() {
		return type;
	}

	public void setType(MealType type) {
		if(this.type != null && this.type.equals(type)) return;		
		MealType typeOld = this.type;
		this.type = type;	
		propertyChangeSupport.fireIndexedPropertyChange("type", PCE_INDEX_type,typeOld,type);
	}
	public Iterator<Ingredient> iteratorIngredients() {
		return ingredients.iterator();
	}

	public void setIngredients(Collection<Ingredient> ingredients) {		
		this.ingredients.clear();
		boolean change = this.ingredients.addAll(ingredients);
		Iterator<Ingredient> iterator = iteratorIngredients();
		if(change){
			propertyChangeSupport.fireIndexedPropertyChange("ingredients", PCE_INDEX_ingredients,null,ingredients);
		}
		while (iterator.hasNext()) {
			Ingredient ingredient = (Ingredient) iterator.next();
			ingredient.propertyChangeSupport.addPropertyChangeListener(pca);
		}		
	}
	public boolean addIngredient(Ingredient ingredient) {
		boolean change = ingredients.add(ingredient);
		ingredient.propertyChangeSupport.addPropertyChangeListener(pca);
		if(change){
			propertyChangeSupport.fireIndexedPropertyChange("ingredients", PCE_INDEX_ingredients,null,ingredients);
		}
		return change;
	}
	public boolean removeIngredient(Ingredient ingredient) {
		boolean change =  ingredients.remove(ingredient);
		if(change){
			propertyChangeSupport.fireIndexedPropertyChange("ingredients", PCE_INDEX_ingredients,null,ingredients);
		}
		return change;
	}

	private class PropertyChangeAdvancement implements PropertyChangeListener{
		@Override
		public void propertyChange(PropertyChangeEvent evt) {
			propertyChangeSupport.fireIndexedPropertyChange(
					"ingredients", PCE_INDEX_ingredients, null,ingredients);			
		}		
	}

	
	
	

	

	

	
}
